package com.har.modules.web.controller;

import com.har.common.utils.MapUtils;
import com.har.common.utils.ValidateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 图形验证码
 *
 * @author maorui
 */
@Slf4j
@RestController
@RequestMapping("/image")
public class ValidateCodeController {



    /**
     * 响应验证码页面
     * @return
     */
    @RequestMapping(value="/validateCode")
    public String validateCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //生成图形验证码
        ValidateCode vCode = new ValidateCode(80,40,4,80);
        MapUtils.maps.put("nCode", vCode.getCode());
        log.info("nCode>>>>{}",vCode.getCode());
        vCode.write(response.getOutputStream());
        return null;
    }


}
